﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace SimpleAppServer.Core
{
    /// <summary>
    /// 抽象的AppServer基类
    /// </summary>
    public abstract class AbstractAppServer:IAppServer
    {
        protected ILog logger = Logger.GetLogger("AppServer");      

        event EventHandler<ReceivedArgs> m_Received;
        /// <summary>
        /// 数据接收事件
        /// </summary>
        public event EventHandler<ReceivedArgs> Received
        {
            add
            {
                m_Received += value;
            }
            remove
            {
                m_Received -= value;
            }
        }

        /// <summary>
        /// 数据接收
        /// </summary>
        /// <param name="args">ReceiveArgs</param>
        protected virtual void OnReceived(ReceivedArgs args)
        {
            if (m_Received != null)
            {
                m_Received(this, args);
            }
        }

        event EventHandler<AppSessionArgs> m_RegistSession;
        /// <summary>
        /// Session注册事件
        /// </summary>
        public event EventHandler<AppSessionArgs> RegistSession
        {
            add
            {
                m_RegistSession += value;
            }
            remove
            {
                m_RegistSession -= value;
            }
        }

        /// <summary>
        /// Session注册
        /// </summary>
        /// <param name="args">AppSessionArgs</param>
        protected virtual void OnRegistSession(AppSessionArgs args)
        {
            if (m_RegistSession != null)
            {
                m_RegistSession(this, args);
            }
        }

        event EventHandler<AppSessionArgs> m_UnRegistSession;
        /// <summary>
        /// Session注销事件
        /// </summary>
        public event EventHandler<AppSessionArgs> UnRegistSession
        {
            add
            {
                m_UnRegistSession += value;
            }
            remove
            {
                m_UnRegistSession -= value;
            }
        }

        /// <summary>
        /// Session注销
        /// </summary>
        /// <param name="args">AppSessionArgs</param>
        protected virtual void OnUnRegistSession(AppSessionArgs args)
        {
            if (m_UnRegistSession != null)
            {
                m_UnRegistSession(this, args);
            }
        }

        /// <summary>
        /// 是否运行中
        /// </summary>
        public abstract bool IsRunning
        {
            get;
        }

        /// <summary>
        /// 安装配置
        /// </summary>
        /// <param name="config">配置</param>
        public abstract void Setup(IServerConfig config);

        /// <summary>
        /// 开启服务
        /// </summary>
        /// <returns>开启成功或失败</returns>
        public abstract bool Start();

        /// <summary>
        /// 结束服务
        /// </summary>
        public abstract void End();

        /// <summary>
        /// 获取所有AppSession
        /// </summary>
        /// <returns></returns>
        public virtual IList<IAppSession> GetAllSession()
        {
            return new List<IAppSession>();
        }

        /// <summary>
        /// 创建AppSession
        /// </summary>
        /// <param name="token">UserToken</param>
        /// <returns>IAppSession</returns>
        public virtual IAppSession CreateSession(object token=null)
        {
            throw new NotSupportedException("Create AppSession");
        }
    }
}
